package cn.javaxc.mvcwork.controller;
import cn.javaxc.mvcwork.controller.*;
import cn.javaxc.mvcwork.util.common.ObjectUtils;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import cn.javaxc.mvcwork.framework.actions.TActionContext;
/**
 * 框架的主校验类<br/>
 * 进行必须性校验.<br/>
 * 格式校验:如字符串格式,数字格式等<br/>
 *
 *
 */
public class GenericValidator {

  public static int REQUIRED_FIELD = 2004090301;
  public static int IS_BEFORE_TODAY = 2004090302;
  public static int PUBLIC_ACCESS_REQUIRED = 2004090303;
  public static int INVALID_DATE = 2004090801;
  public static int INVALID_NUMBER = 2004091001;
  public static int INVALID_EMAIL = 2004091002;
  public static int INVALID_NOT_REQUIRED_DATE = 2004091003;
  public static int INVALID_NUMBER_NOT_REQUIRED = 2004091004;

  public static int INVALID_EMAIL_NOT_REQUIRED = 2005060801;
  public static int PAST_DATE = 2006110117;


  
  public static void addError(SystemStatus systemStatus, Object object, String field, int errorType) {
    if (errorType == REQUIRED_FIELD) {
      addError(systemStatus, object, field, "必填项");
    } else if (errorType == PAST_DATE) {
      addError(systemStatus, object, field, "日期不可以是过去");
    } else if (errorType == INVALID_DATE || errorType == INVALID_NOT_REQUIRED_DATE) {
      addError(systemStatus, object, field, "日期必须按照当地服务器日期格式");
    } else if (errorType == INVALID_NUMBER||errorType == INVALID_NUMBER_NOT_REQUIRED) {
      addError(systemStatus, object, field, "数字必须使用恰当的数字格式");
    } else if (errorType == INVALID_EMAIL || errorType == INVALID_EMAIL_NOT_REQUIRED) {
      addError(systemStatus, object, field, "邮箱地址必须以适当格式");
    }
  }


  
  public static void addError(SystemStatus systemStatus, Object object, String field, String errorKey) {
    HashMap errors = (HashMap) ObjectUtils.getObject(object, "formErrors");
    if (systemStatus != null) {
      errors.put(field, systemStatus.getLabel(errorKey));
    } else {
      errors.put(field, errorKey);
    }
  }


  
  public static void checkWarning(SystemStatus systemStatus, Object object, String field, int warningType) {
    if (warningType == IS_BEFORE_TODAY) {
      Timestamp result = (Timestamp) ObjectUtils.getObject(object, field);
      if (result != null && result.before(new java.util.Date())) {
        addWarning(
            systemStatus, object, field, "日期为今天或之前");
      }
    }
  }


  
  public static void addWarning(SystemStatus systemStatus, Object object, String field, String warningKey) {
    HashMap warnings = (HashMap) ObjectUtils.getObject(object, "formWarnings");
    if (systemStatus != null) {
      warnings.put(field, systemStatus.getLabel(warningKey));
    } else {
      warnings.put(field, warningKey);
    }
  }


  
  public static void checkLength(SystemStatus systemStatus, Object object, String errorName, String fieldName, int length) {
    String value = (String) ObjectUtils.getObject(object, fieldName);
    if (value != null && value.length() > length) {
      addError(systemStatus, object, fieldName, errorName);
    }
  }
  public static boolean checkError(TActionContext context, String field, int errorType){
	return checkError(AppUtils.getSystemStatus(context),context.getRequest() ,context.getFormBean(),field,errorType);
  }
  public static boolean checkError(SystemStatus systemStatus, HttpServletRequest request,Object object, String field, int errorType)
  {
    boolean returnValue = true;
    if (errorType == REQUIRED_FIELD) {
      String result = request.getParameter(field);
      if (result == null || "".equals(result.trim())) {
        returnValue = false;
        addError(systemStatus, object, field, REQUIRED_FIELD);
      }
	} else if (errorType == INVALID_NUMBER) {
        String date = request.getParameter(field);
        if (date == null || "".equals(date.trim()) || "-1".equals(date.trim()) ) {
          returnValue = false;
          addError(systemStatus, object, field, REQUIRED_FIELD);
        } else {
          try {
			Integer.parseInt(date);
          } catch (Exception e1) {
            returnValue = false;
            addError(systemStatus, object, field, INVALID_NUMBER);
          }
        }
    } else if (errorType == INVALID_NUMBER_NOT_REQUIRED) {
        String date = request.getParameter(field);
        if (date != null && !"".equals(date.trim())) {
			try{
            Integer.parseInt(date);
          } catch (Exception e1) {
            returnValue = false;
            addError(systemStatus, object, field, INVALID_NUMBER);
          }
		}
    } else if (errorType == INVALID_DATE) {
        String date = request.getParameter(field);
        if (date == null || "".equals(date.trim())) {
          returnValue = false;
          addError(systemStatus, object, field, REQUIRED_FIELD);
        } else {
          try {
//            Locale locale = new Locale(
//                System.getProperty("LANGUAGE"), System.getProperty("COUNTRY"));
            SimpleDateFormat localeFormatter = new SimpleDateFormat("yyyy-MM-dd");
            localeFormatter.applyPattern("yyyy-MM-dd");
            localeFormatter.setLenient(false);
            localeFormatter.parse(date);
          } catch (Exception e1) {
            returnValue = false;
            addError(systemStatus, object, field, INVALID_DATE);
          }
        }
    } else if (errorType == INVALID_NOT_REQUIRED_DATE) {
        String date = request.getParameter(field);
        if (date != null && !"".equals(date.trim())) {
			try{
//            Locale locale = new Locale(
//                System.getProperty("LANGUAGE"), System.getProperty("COUNTRY"));
            SimpleDateFormat localeFormatter = new SimpleDateFormat("yyyy-MM-dd");
            localeFormatter.applyPattern("yyyy-MM-dd");
            localeFormatter.setLenient(false);
            localeFormatter.parse(date);
          } catch (Exception e1) {
            returnValue = false;
            addError(systemStatus, object, field, INVALID_DATE);
          }
		}
    }
    return returnValue;
  }

}
